package com.wangyadong.hobby.schedule.jobtask.javasync;

import org.omg.CORBA.PRIVATE_MEMBER;

import java.util.stream.IntStream;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/11/1413:22
 **/
public class DeadLock {

    private   Object READ = new Object();
    private   Object WRITE = new Object();


    public   void read() {
        synchronized (READ) {
            System.out.println(Thread.currentThread().getName() + "  read");
            synchronized (WRITE) {
                System.out.println(Thread.currentThread().getName() + " write in read");
            }
        }
    }

    public   void write() {
        synchronized (WRITE) {
            System.out.println(Thread.currentThread().getName() + "  write");
            synchronized (READ) {
                System.out.println(Thread.currentThread().getName() + " read in write  ");
            }
        }
    }

    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
        IntStream.range(0, 10).forEach(i -> {
            new Thread(()->{
                deadLock.write();
            }).start();
            new Thread(()->{
                deadLock.read();
            }).start();
        });
    }
}
